// pages/personalData/personalData.js
// import Toast from 'path/to/@vant/weapp/dist/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:'',
    dataList:'',   //个人信息列表
    acceptAddress:[],//收货地址
    // 个人信息
    name:'',
    phone:'',
    province:'',
    status:'',
    channel:'',
    distributor:'',
    acname:'',
    acphone:'',
    acaddress:'',
    acaddressid:'',
    date: '',
    successShow:false,//修改成功的弹出框
    option1: [
      { text: '电工', value: 0 },
      { text: '项目经理', value: 1 },
      { text: '其他', value: 2 },
    ],
    value1: 0,
  },
  changeDate(e){
    this.setData({ date:e.detail.value});
    },
  // 修改成功的弹出框关闭
  editsuccessclose:function(){
    this.setData({
      successShow: false
    })
  },
  // 实时更新姓名输入信息
  bindKeyInputname:function(e){
    this.setData({
      name: e.detail.value
    })
  },
  // 实时更新电话输入信息
  bindKeyInputphone:function(e){
    this.setData({
      phone: e.detail.value
    })
  },
  // 实时更新省市输入信息
  bindKeyInputprovince: function (e) {
    this.setData({
      province: e.detail.value
    })
  },
  // 职业下拉列表
  onChangeOccupation(event) {
    this.setData({
      value1: event.detail
    })
  },
  // 采购渠道线上线下修改
  onChange(event) {
    // console.log('event.detail',event.detail)
    this.setData({
      channel: event.detail,
    });
  },
  // 实时更新经常采购的经销商
  bindKeyInputdistributor:function(e){
    this.setData({
      distributor: e.detail.value,
    });
  },
  // 收件信息的姓名的实时数据更新
  bindKeyInputacname:function(e){
    this.setData({
      acname: e.detail.value,
    });
  },
  // 收件信息的电话的实时数据更新
  bindKeyInputacphone:function(e){
    this.setData({
      acphone: e.detail.value,
    });
  },
  // 收件信息的地址的实时数据更新
  bindKeyInputacaddress:function(e){
    this.setData({
      acaddress: e.detail.value,
    });
  },
  // 修改提交的数据
  submit(){
    
    var that = this;
    for(var i = 0;i < that.data.option1.length;i++){
      if(that.data.option1[i].value == that.data.value1){
        that.setData({
          status:that.data.option1[i].text
        })
      }
    }
    var mtdata = {
      openid:that.data.openid,
      name:that.data.name,
      phone:that.data.phone,
      city:that.data.province,
      status:that.data.status,
      channel:that.data.channel,
      distributor:that.data.distributor,
      address:{
          addressid:that.data.acaddressid,
          name:that.data.acname,
          phone:that.data.acphone,
          address:that.data.acaddress
      }
    }
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/user/update',
      data:mtdata,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        if(res.data.flag){
         // 个人信息修改成功的弹出层显示
          // that.setData({
          //   successShow: true
          // })
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 1000
           })
            
           setTimeout(function(){
            wx.hideToast()
           },1000)
        }
      },
      fail: function() {
        // fail
        
      },
      complete: function() {
        // complete
      }
    })
  },
  // 进入页面获取的数据
  getList:function(){
    this.setData({ 
      openid:getApp().globalData.openid
    })
    var that = this
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/user/selectByOpenid',
      data: {
        openid:this.data.openid
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        console.log('res',res.data.data)
        if(res.data.flag){
          that.setData({ 
            dataList:res.data.data,
            // 个人信息
            name:res.data.data.name,
            phone:res.data.data.phone,
            province:res.data.data.city,
            status:res.data.data.status,
            channel:JSON.stringify(res.data.data.channel),
            distributor:res.data.data.distributor
          })
          for(var i = 0;i < that.data.option1.length;i++){
            if(that.data.option1[i].text == that.data.status){
              that.setData({
                value1:that.data.option1[i].value
              })
            }
          }
          for(var i=0;i<=res.data.data.list.length;i++){
            if(res.data.data.list[i].prefer == 1){
                that.setData({
                  acname:res.data.data.list[i].name,
                  acphone:res.data.data.list[i].phone,
                  acaddress:res.data.data.list[i].address,
                  acaddressid:res.data.data.list[i].addressid
                })
              return
            }
          }
        }else{
          console.log('thatthatthatthat')
        }
      },
      fail: function() {
        // fail
        
      },
      complete: function() {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})